load("//rs/tests:common.bzl", "GRAFANA_RUNTIME_DEPS", "NNS_CANISTER_ENV", "NNS_CANISTER_RUNTIME_DEPS", "UNIVERSAL_CANISTER_ENV", "UNIVERSAL_CANISTER_RUNTIME_DEPS", "UNIVERSAL_VM_RUNTIME_DEPS")
load("//rs/tests:system_tests.bzl", "oci_tar", "system_test", "system_test_nns", "uvm_config_image")

package(default_visibility = ["//rs:system-tests-pkg"])

system_test(
    name = "compute_allocation_test",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        "//rs/config",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:futures",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-utils",
    ],
)

system_test(
    name = "cycles_restrictions_test",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-utils",
    ],
)

system_test(
    name = "cycles_cost_schedule_test",
    env = NNS_CANISTER_ENV | UNIVERSAL_CANISTER_ENV,
    tags = [
        "long_test",
    ],
    runtime_deps = NNS_CANISTER_RUNTIME_DEPS + UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/governance/api",
        "//rs/nns/test_utils",
        "//rs/registry/canister",
        "//rs/registry/nns_data_provider",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:ic-utils",
        "@crate_index//:slog",
    ],
)

system_test(
    name = "general_execution_test",
    srcs = glob(["general_execution_tests/*.rs"]),
    crate_root = "general_execution_test.rs",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        "//rs/config",
        "//rs/crypto/sha2",
        "//rs/cycles_account_manager",
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "//rs/types/types_test_utils",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-cdk",
        "@crate_index//:ic-utils",
        "@crate_index//:lazy_static",
        "@crate_index//:rand",
        "@crate_index//:rand_chacha",
        "@crate_index//:reqwest",
        "@crate_index//:serde_cbor",
        "@crate_index//:serde_json",
        "@crate_index//:slog",
        "@crate_index//:tokio",
        "@crate_index//:wat",
    ],
)

system_test(
    name = "inter_canister_queries_test",
    srcs = glob(["inter_canister_queries_tests/*.rs"]),
    crate_root = "inter_canister_queries_test.rs",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:assert_matches",
        "@crate_index//:ic-agent",
        "@crate_index//:wat",
    ],
)

system_test(
    name = "max_number_of_canisters_test",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-utils",
    ],
)

system_test_nns(
    name = "canister_migration_test",
    enable_mainnet_nns_variant = False,
    env = UNIVERSAL_CANISTER_ENV,
    tags = [
        "long_test",
    ],
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        "//rs/nns/constants",
        "//rs/nns/test_utils",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:futures",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-utils",
        "@crate_index//:serde",
        "@crate_index//:slog",
        "@crate_index//:tokio",
        "@crate_index//:tokio-util",
    ],
)

system_test(
    name = "system_api_security_test",
    deps = [
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-utils",
        "@crate_index//:slog",
        "@crate_index//:tokio",
        "@crate_index//:wat",
    ],
)

system_test(
    name = "system_subnets_test",
    env = UNIVERSAL_CANISTER_ENV,
    runtime_deps = UNIVERSAL_CANISTER_RUNTIME_DEPS,
    deps = [
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-cdk",
        "@crate_index//:ic-utils",
        "@crate_index//:slog",
    ],
)

system_test(
    name = "fill_execution_rounds_workload",
    tags = [
        "manual",
    ],
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + [
        "//rs/tests:jaeger_uvm_config_image",
    ],
    deps = [
        "//rs/registry/routing_table",
        "//rs/registry/subnet_type",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:futures",
        "@crate_index//:slog",
    ],
)

exports_files([
    "btc_integration/bitcoin.conf",
    "btc_integration/btc_activate.sh",
])

uvm_config_image(
    name = "btc_uvm_config_image",
    srcs = [
        ":bitcoind.tar",
        ":btc_integration/bitcoin.conf",
        ":btc_integration/btc_activate.sh",
    ],
    remap_paths = {
        "btc_activate.sh": "activate",
    },
    tags = ["manual"],  # this target will be built if required as a dependency of another target
)

oci_tar(
    name = "bitcoind.tar",
    image = "@bitcoind",
    repo_tags = ["bitcoind:pinned"],
)

BTC_RUNTIME_DEPS = [
    # Keep sorted.
    ":btc_uvm_config_image",
    "@btc_canister//file",
]

system_test_nns(
    name = "btc_get_balance_test",
    env = UNIVERSAL_CANISTER_ENV | {
        "BTC_WASM_PATH": "$(rootpath @btc_canister//file)",
    },
    flaky = True,  # flakiness rate of over 3% over the month from 2025-02-11 till 2025-03-11 only for the //rs/tests/execution:btc_get_balance_test_head_nns variant.
    tags = [
        "colocate",
        "long_test",  # the P90 duration was over 7 minutes for the week starting on 2025-08-25.
    ],
    runtime_deps =
        UNIVERSAL_VM_RUNTIME_DEPS +
        UNIVERSAL_CANISTER_RUNTIME_DEPS +
        BTC_RUNTIME_DEPS,
    deps = [
        "//rs/bitcoin/adapter/test_utils",
        "//rs/registry/subnet_type",
        "//rs/tests/ckbtc",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "//rs/universal_canister/lib",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:slog",
    ],
)
